<?php
namespace Admin\Model;

use Admin\AppConst\WebConst;
use Think\Model;

//用户model
class RoleModel extends BaseModel
{
    /**
     * @var RoleModel
     */
    private     static $instance;
    protected   $tableName  = 'Role';
    private     $model;
    private     $where;


    //定义所有的表字段
    private $id         = 'id';
    private $title      = 'title';
    private $content    = 'content';
    private $meunIds    = 'menu_ids';
    private $addtime    = 'addtime';

    /**
     * Get the instance of RoleModel.
     * @return RoleModel
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new RoleModel();
        }
        return self::$instance;
    }

    public function __construct(){
        parent::__construct();
        $this->where    = $this->setDefaultWhere();
        $this->model    = M($this->tableName);
    }

    /* @function    getList   //获取菜单数据
     * @date        2018-07-16
     * @author      zhangle
     * @return      array
     */

    public function getList(){
        return $this->model->select();
    }

    /**
     * @function    createRole  创建
     * @param       $title      string  名称
     * @param       $content    string  说明
     * @param       $menuIdList array   菜单id
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function createRole($title, $content, $menuIdList){
        $save   = array(
            $this->title    => $title,
            $this->content  => $content,
            $this->meunIds  => $menuIdList,
            $this->addtime  => time(),
        );
        return $this->model->add($save);
    }

    /**
     * @function    getRoleById         查询单个
     * @param       $id         int     id
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function getRoleById($id){
        $where  = array(
            $this->id   => $id,
        );

        return $this->model->where($where)->find();
    }


    /**
     * @function    editRole    更新
     * @param       $id         int     id
     * @param       $title      string  名称
     * @param       $content    string  说明
     * @param       $menuIdList array   菜单id
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function editRole($id, $title, $content, $menuIdList){
        $where  = array(
            $this->id   => $id,
        );
        $save   = array(
            $this->title    => $title,
            $this->content  => $content,
            $this->meunIds  => $menuIdList,
            $this->addtime  => time(),
        );
        return $this->model->where($where)->save($save);
    }

    /**
     * @function    deleteRole  删除
     * @param       $id         int     id
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function deleteRole($id){
        $where  = array(
            $this->id   => $id,
        );
        return $this->model->where($where)->delete();
    }
}